package com.c.ctgproject.wiget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.PhotoShowListAdapter
import com.c.ctgproject.adapter.PhotoShowListAdapterV2
import java.lang.StringBuilder

class PhotoListShowRecyclerView : RecyclerView, PhotoShowListAdapterV2.PhotoShowListCallBack {
    var photoList = ArrayList<String>()
    val fragment = context as FragmentActivity
    var lineNum = 3
    var max = 1
    var isEdit = true
    private var nowPosition = 1
    private var listener:PhotoListShowCallBack?=null
    constructor(context: Context) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

        attrs?.let {
            val a = getContext().obtainStyledAttributes(attrs, R.styleable.PhotoListShowRecyclerView)
            lineNum = a.getInt(R.styleable.PhotoListShowRecyclerView_lineNum, 3)
            max = a.getInt(R.styleable.PhotoListShowRecyclerView_maxNum, 1)
            isEdit = a.getBoolean(R.styleable.PhotoListShowRecyclerView_isEdit, true)
        }
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }


//    private var list: MutableList<Int>? = null
//    var textView: TextView? = null
    //车牌

    private fun initView(context: Context) {
        init(context)
    }
    fun clearPhotos(){
        photoList.clear()
    }
    fun addPhotos(urls: List<String>){
        photoList.addAll(urls)
        adapter?.notifyDataSetChanged()
    }
    fun setPhotoListShowCallBack(listener:PhotoListShowCallBack){
        this.listener=listener
    }
    var photoShowListAdapter: PhotoShowListAdapterV2? = null
    private fun init(context: Context) {
//        list = ArrayList()
//        for (i in 0..11) {
//            if (i == 10) {
//                list.add(0)
//            } else {
//                list.add(i + 1)
//            }
//        }
//        demoAdapter = DemoAdapter(context, list)
//        recyclerView.setLayoutManager(GridLayoutManager(context, 4))
//        recyclerView.setAdapter(demoAdapter)
        photoShowListAdapter = PhotoShowListAdapterV2(context as FragmentActivity, photoList, max, isEdit, this)
        this?.layoutManager = StaggeredGridLayoutManager(lineNum, StaggeredGridLayoutManager.VERTICAL)
        this?.adapter = photoShowListAdapter
        this?.adapter?.notifyDataSetChanged()
    }

    override fun deleteItem(url: String, position: Int) {
        listener?.deletePhoto(url)
    }
    fun getPhotos():ArrayList<String>{
        return photoList
    }
    override fun choosePhotoItem(urls: List<String>) {
        if(listener?.selectedPhoto(urls)!=false){
            addPhotos(urls)
        }
    }
    interface PhotoListShowCallBack{
        //当需要处理后再显示时候用addPhotos记得返回false
        fun selectedPhoto(urls: List<String>):Boolean
        fun deletePhoto(url:String):Boolean
    }
}
